﻿using System;
using MSAcademic.v2.Database;
using System.Collections.Generic;
using System.Linq;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.Article.Impl;
using MSAcademic.v2.Infrastructure.SubSection;
using MSAcademic.v2.Infrastructure.SubSection.Impl;

namespace MSAcademic.v2.Infrastructure.ContentSection.Impl
{
    public class ContenSectionQueriesImpl : IContenSectionQueries
    {
        public IEnumerable<IContentSection> GetAll()
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbContentSectionList = db.ContentSections
                    .Where(x => x.Id != null)
                    .OrderByDescending(x => x.Name)
                    .Select(x => new ContentSectionImpl
                    {
                        Id = x.Id,
                        Name = x.Name
                    }).ToList();

                return dbContentSectionList;

            }
        }

        public List<Tuple<IContentSection, List<ISubSection>>> GetAllContentSectionsWithSubSections(bool showHidden)
        {
            var tupleList = new List<Tuple<IContentSection, List<ISubSection>>>();

            using(var db= new MSAcademicDatabaseContainer())
            {
                var contentSections = db.ContentSections;
                
                foreach (var c in contentSections)
                {
                    var tuple = new Tuple<IContentSection, List<ISubSection>>(new ContentSectionImpl { Id = c.Id, Name = c.Name }, new List<ISubSection>());
                    var subSections = c.SubSections.Where(m=>m.IsHidden==showHidden);
                    foreach (var s in subSections)
                    {
                       tuple.Item2.Add(new SubSectionImpl{ContentSectionId = s.ContentSectionId,Id = s.Id,IsHidden = s.IsHidden,Name = s.Name,ThumbnailUrl = s.ThumbnailUrl});

                    }
                    tupleList.Add(tuple);
                }
            }
            return tupleList;
        }

        public List<Tuple<ISubSection, List<IArticle>>> GetDataForContentSection(int contentSectionId,bool showHidden)
        {
            var tupleList = new List<Tuple<ISubSection, List<IArticle>>>();
            using(var db= new MSAcademicDatabaseContainer())
            {
                var contentSection = db.ContentSections.SingleOrDefault(m => m.Id == contentSectionId);
                if (contentSection== null) return null;

                var subsections = contentSection.SubSections.Where(m=>m.IsHidden==showHidden).ToList();
                foreach (var s in subsections)
                {
                    var articlesInSubSection =
                        s.Articles.Where(m=>m.IsHidden==false).OrderByDescending(m => m.PublishedOn).Select(m=> new ArticleImpl
                        {
                            CreatedOn = m.CreatedOn,
                            Id = m.Id,IsHidden = m.IsHidden,
                            LongText = m.LongText,
                            PictureUrlBig = m.PictureUrlBig,
                            PictureUrlNormal = m.PictueUrlNormal,
                            PictureUrlThumbnail = m.PictureUrlThumbnail
                            ,PublishedOn = m.PublishedOn,
                            ShortText = m.ShortText,
                            Title = m.Title}).Cast<IArticle>().ToList();

                    var tuple =
                        new Tuple<ISubSection, List<IArticle>> (
                            new SubSectionImpl
                                {
                                    ContentSectionId = s.ContentSectionId,
                                    Id = s.Id,
                                    IsHidden = s.IsHidden,
                                    Name = s.Name,
                                    ThumbnailUrl = s.ThumbnailUrl
                                }, articlesInSubSection);
                    
                    tupleList.Add((tuple));
                }
                return tupleList;


            }
        }
    }
}
